package face.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import face.pojo.User;
import face.service.UserService;
import face.util.CipherUtil;

@Controller
public class UserController {
	@Resource
	private UserService userService;
	//跳转登陆
	@RequestMapping("/login")
	public String login() {
		return "login";
	}
	//登陆验证
	@RequestMapping("login.html")
	public ModelAndView login(HttpSession session, User user) {
		//md5加密数据
		String pwd = user.getUserPwd();
		String Pwd2 = "";
		CipherUtil cipher = new CipherUtil();
		Pwd2 = cipher.generatePassword(pwd);
		System.err.println("MD5加密后:"+Pwd2);
		
		user = userService.getUser(user);
		if (user.getUserPwd().equals(Pwd2)) {
			session.setAttribute("user", user);
			System.out.println("登陆的账号是:" + user.getUserName());
			System.out.println("登陆的密码是:" + user.getUserPwd());
			
			return new ModelAndView("index");
		}
		return new ModelAndView("login");
	}
	//跳转到拍照
	@RequestMapping("camera.html")
	public String camera(){
		return "camera";
	}
	//跳转访客管理
	@RequestMapping("list.html")
	public String list(){
		return "list";
	}
	//跳转增加访客页面
	@RequestMapping("add.html")
	public String add(){
		return "add";
	}
	//跳转修改密码
	@RequestMapping("pass.html")
	public String pass(){
		return "pass";
	}
	//跳转到首页
	@RequestMapping("index.html")
	public String index(){
		return "index";
	}
	//退出账号
	@RequestMapping("exit.html")
	public ModelAndView exit(HttpSession session) {
		session.invalidate();
		return new ModelAndView("login");
		
	}
	
	//用户修改密码
	@RequestMapping("repass.html")
	public ModelAndView repass(HttpSession session, User user) {
		System.out.println("session里的值:"+session.getAttribute(user.getUserName()));
		return new ModelAndView("pass"); 
	}
}
